package com.example.bnuzzu.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.system.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bnuzzu.common.dto.*;
import com.example.bnuzzu.common.lang.Result;
import com.example.bnuzzu.entity.*;
import com.example.bnuzzu.mapper.UserinfoMapper;
import com.example.bnuzzu.service.*;
import com.example.bnuzzu.util.ShiroUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BNUZZU
 * @since 2021-04-13
 */
@RestController
public class UserinfoController {
    @Autowired
    UserinfoService userinfoService;
    @Autowired
    UserimageService userimageService;
    @Autowired
    FavoritehouseService favoritehouseService;
    @Autowired
    HouseinfoService houseinfoService;


    @PostMapping(value = "/userInfoEdit")
    @RequiresAuthentication
    public Result EditInfo(@RequestBody UserDto user)
    {
        //判断是否修改自己的信息
        if(user.getUserid() != null){
            Assert.isTrue(user.getUserid() == ShiroUtil.getProfile().getUserid().intValue(), "没有权限编辑");
        }else {
            return Result.fail(500,"请重新登录",null);
        }
        Userinfo userinfo=new Userinfo();
        Userimage userimage = new Userimage();
        BeanUtil.copyProperties(user,userinfo);
        BeanUtil.copyProperties(user,userimage);

        userinfoService.saveOrUpdate(userinfo);
        userimageService.saveOrUpdate(userimage);

        return  Result.success(null);
    }

    @PostMapping(value = "/passwordEdit/{pwd}")
    @RequiresAuthentication
    public Result EditPwd(@RequestBody Userinfo user,@PathVariable(name = "pwd") String pwd)
    {
        //判断是否修改自己的信息
        if(user.getUserid() != null){
            Assert.isTrue(user.getUserid() == ShiroUtil.getProfile().getUserid().intValue(), "没有权限编辑");
        }else {
            return Result.fail(500,"请重新登录",null);
        }

        Userinfo userinfo = userinfoService.getById(user.getUserid());
        if(!userinfo.getPassword().equals(SecureUtil.md5(user.getPassword()))){
            return Result.fail(500,"旧密码不正确",null);
        }else {
            userinfo.setPassword(SecureUtil.md5(pwd));
            userinfoService.updateById(userinfo);
        }

        return  Result.success(null);
    }

    @GetMapping(value = "/getUserInfo/{id}")
    public Result GetInfo(@PathVariable(name = "id") Integer userid)
    {
        //判断是否查询自己的信息
        if(userid != null){
            Assert.isTrue(userid == ShiroUtil.getProfile().getUserid().intValue(), "没有权限编辑");
        }else {
            return Result.fail(500,"请重新登录",null);
        }
        Userinfo userinfo = userinfoService.getById(userid);
        Userimage userimage = userimageService.getById(userid);
        String image = userimage.getUserimage();
        Map<String,Object> map = new LinkedHashMap<>();
        UserDto userDto = new UserDto();
        BeanUtil.copyProperties(userinfo,userDto);
        userDto.setUserimage(image);
        map.put("userinfo",userDto);
        return Result.success(map);
    }

    @RequiresAuthentication
    @PostMapping("/UserFavorites/{page}")
    public Result list(@RequestBody Userinfo user,@PathVariable(name = "page", required = false) Integer p){

        if (user.getUserid() != null){
            Assert.isTrue(user.getUserid() == ShiroUtil.getProfile().getUserid().intValue(), "没有权限编辑");
        }else {
            return Result.fail(500,"请重新登录",null);
        }
        QueryWrapper<Favoritehouse> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("userid",user.getUserid());
        Page<FavoriteDto> page = new Page(p,10);
        IPage<FavoriteDto> pagedata = favoritehouseService.selectFavoritePage(page,user.getUserid());
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("pageData",pagedata);
        return Result.success(map);
    }

    @RequiresAuthentication
    @PostMapping("/AddFavorites")
    public Result AddFavor(@RequestBody Favoritehouse favoritehouse){

        if(favoritehouse.getUserid() != null){
            Assert.isTrue(favoritehouse.getUserid() == ShiroUtil.getProfile().getUserid().intValue(), "没有权限编辑");
        }else {
            return Result.fail(500,"请重新登录",null);
        }
        favoritehouseService.save(favoritehouse);
        return Result.success(null);


    }
    @RequiresAuthentication
    @PostMapping("/DeleteFavorites")
    public Result DeleteFavor(@RequestBody Favoritehouse favoritehouse){

        if(favoritehouse.getUserid() != null){
            Assert.isTrue(favoritehouse.getUserid() == ShiroUtil.getProfile().getUserid().intValue(), "没有权限编辑");
        }else {
            return Result.fail(500,"请重新登录",null);
        }
        QueryWrapper<Favoritehouse> wrapper = new QueryWrapper<>();
        wrapper.eq("houseid",favoritehouse.getHouseid())
                .eq("userid",favoritehouse.getUserid());
        favoritehouse = favoritehouseService.getOne(wrapper);

        favoritehouseService.removeById(favoritehouse.getFavoriteid());
        return Result.success(null);
    }

}



